/*
 * @Description: 文件内容描述
 * @Author: 李志杰
 * @Date: 2020-03-04 19:07:08
 * @LastEditTime: 2020-05-19 17:34:42
 * @LastEditors: huacong
 */
import _ from 'lodash';
import * as apis from '@/services/ProjectWorkbenchMgr';

const company = 100000000;
const Model = {
  namespace: 'Workbench',
  state: {
    allProjectIndexUsingItems: [
      {
        title: '项目总数(个)',
        value: 0,
        precision: 0,
        content: [
          {
            title: '首开项目',
            value: 0,
            suffix: '个',
            precision1: 0,
          },
          {
            title: '续销项目',
            value: 0,
            suffix: '个',
            precision1: 0,
          },
        ],
      },
      {
        className: 'produce',
        title: '总货值(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            title: '权益总货值',
            value: 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '阶段自持货值',
            value: 0,
            suffix: '亿',
            precision1: 2,
          },
        ],
      },
      {
        className: 'supply',
        title: '已售货值(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            title: '权益已售货值',
            value: 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'market',
        title: '存货货值(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            title: '权益存货货值',
            value: 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'deposit',
        title: '规划货值(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            title: '权益在途货值',
            value: 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
      {
        className: 'junction',
        title: '土储货值|香港口径(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            title: '权益土储货值',
            value: 0,
            suffix: '亿',
            precision1: 2,
          },
          {
            title: '权益比例',
            value: 0,
            suffix: '%',
            precision1: 2,
          },
        ],
      },
    ], // 工作台 全部指标数据
    projectIndexUsingItems: [
      {
        title: '总货值(不含未推售车位)(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            value1: 0,
            value2: 0,
            suffix1: '套',
            suffix2: '个',
          },
        ],
      },
      {
        className: 'produce',
        title: '已售货值(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            value1: 0,
            value2: 0,
            suffix1: '套',
            suffix2: '个',
          },
        ],
      },
      {
        className: 'supply',
        title: '存货货值(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            value1: 0,
            value2: 0,
            suffix1: '套',
            suffix2: '个',
          },
        ],
      },
      {
        className: 'market',
        title: '规划货值(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            value1: 0,
            value2: 0,
            suffix1: '套',
            suffix2: '个',
          },
        ],
      },
      {
        className: 'deposit',
        title: '土储货值|香港口径(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            value1: 0,
            value2: 0,
            suffix1: '套',
            suffix2: '个',
          },
        ],
      },
      {
        className: 'junction',
        title: '未推售车位货值(亿)',
        value: 0,
        precision: 2,
        content: [
          {
            value1: 0,
            value2: 0,
            suffix1: 0,
            suffix2: '个',
          },
        ],
      },
    ], // 工作台 项目指标数据
    porjectStagePageList: [], // 项目概览
    projectUnderwayList: [], // 正在进行
    businessType: [], // 业务类型(下拉框)
    event: [], // 事件(下拉框)
    operationBy: [], // 操作人(下拉框)
    stages: [], // 显示分期(下拉框)
    todoType: [], // 显示门户待办类型(下拉框)
    versionType: [], // 版本类型(下拉框)
    remindItems: {}, // 待办提醒数据
    dynamicItems: {}, // 最新动态数据
    remindParams: {}, // 待办提醒参数
    dynamicParams: {}, // 最新动态参数
    remindLoading: true,
    dynamicLoading: true,
    projectName: '',
  },
  effects: {
    /**
     * @description: 请求工作台顶部项目指标
     * @param : {}
     * @return: 工作台顶部项目指标数据
     */
    *init({ payload, callback }, { call, put, select }) {
      const allProjectIndexUsingItems = yield select(
        state => state.Workbench.allProjectIndexUsingItems,
      );
      const cloneAllProjectIndexUsingItems = _.cloneDeep(allProjectIndexUsingItems);
      const result = yield call(apis.getAllProjectIndexUsingGET, { params: {} });
      if (result && result.code === 1) {
        _.each(cloneAllProjectIndexUsingItems, item => {
          const x = item;
          if (item.title === '项目总数(个)') {
            x.value = result.data.projectTotalCount ? _.toNumber(result.data.projectTotalCount) : 0;
            x.content[0].value = result.data.firstProjectCount
              ? _.toNumber(result.data.firstProjectCount)
              : 0;
            x.content[1].value = result.data.renewalSaleProjectCount
              ? _.toNumber(result.data.renewalSaleProjectCount)
              : 0;
          }
          if (item.title === '总货值(亿)') {
            x.value = result.data.totalValue ? _.toNumber(result.data.totalValue / company) : 0;
            x.content[0].value = result.data.equityTotalValue
              ? _.toNumber(result.data.equityTotalValue / company)
              : 0;
            x.content[1].value = result.data.selfSupportValue
              ? _.toNumber(result.data.selfSupportValue / company)
              : 0;
          }
          if (item.title === '已售货值(亿)') {
            x.value = result.data.soldValue ? _.toNumber(result.data.soldValue / company) : 0;
            x.content[0].value = result.data.equitySoldValue
              ? _.toNumber(result.data.equitySoldValue / company)
              : 0;
            x.content[1].value = result.data.soldEquityRate
              ? _.toNumber(result.data.soldEquityRate * 100)
              : 0;
          }
          if (item.title === '存货货值(亿)') {
            x.value = result.data.stockValue ? _.toNumber(result.data.stockValue / company) : 0;
            x.content[0].value = result.data.equityStockValue
              ? _.toNumber(result.data.equityStockValue / company)
              : 0;
            x.content[1].value = result.data.stockEquityRate
              ? _.toNumber(result.data.stockEquityRate * 100)
              : 0;
          }
          if (item.title === '规划货值(亿)') {
            x.value = result.data.planValue ? _.toNumber(result.data.planValue / company) : 0;
            x.content[0].value = result.data.equityOnTheWayValue
              ? _.toNumber(result.data.equityOnTheWayValue / company)
              : 0;
            x.content[1].value = result.data.planEquityRate
              ? _.toNumber(result.data.planEquityRate * 100)
              : 0;
          }
          if (item.title === '土储货值|香港口径(亿)') {
            x.value = result.data.soilStorageValue
              ? _.toNumber(result.data.soilStorageValue / company)
              : 0;
            x.content[0].value = result.data.equitySoilStorageValue
              ? _.toNumber(result.data.equitySoilStorageValue / company)
              : 0;
            x.content[1].value = result.data.soilStorageEquityRate
              ? _.toNumber(result.data.soilStorageEquityRate * 100)
              : 0;
          }
        });
        yield put({
          type: 'save',
          payload: { allProjectIndexUsingItems: cloneAllProjectIndexUsingItems },
        });
      }
    },
    /**
     * @description: 请求项目概览
     * @param :{projectId}
     * @return: 返回项目概览数据
     */
    *requestProjectStagePageList({ payload, callback }, { call, put }) {
      const result = yield call(apis.getPorjectStagePageListUsingGET, payload);
      if (result && result.code === 1 && result.data) {
        callback(result);
        yield put({
          type: 'save',
          payload: { porjectStagePageList: result.data },
        });
      }
    },
    /**
     * @description: 请求项目工作台
     * @param :{projectId}
     * @return: 返回项目工作台数据
     */
    *requestProjectList({ payload, callback }, { call, put, select }) {
      const projectIndexUsingItems = yield select(state => state.Workbench.projectIndexUsingItems);
      const cloneProjectIndexUsingItems = _.cloneDeep(projectIndexUsingItems);
      // 指标数据
      const result = yield call(apis.getProjectIndexUsingGET, payload);
      if (result && result.code === 1) {
        _.each(cloneProjectIndexUsingItems, item => {
          const x = item;
          if (item.title === '总货值(不含未推售车位)(亿)') {
            x.value = result.data.totalValue ? _.toNumber(result.data.totalValue / company) : 0;
            x.content[0].value1 = result.data.roomNum || 0;
            x.content[0].value2 = result.data.parkingNum || 0;
          }
          if (item.title === '已售货值(亿)') {
            x.value = result.data.soldValue ? _.toNumber(result.data.soldValue / company) : 0;
            x.content[0].value1 = result.data.soldRoomNum || 0;
            x.content[0].value2 = result.data.soldParkingNum || 0;
          }
          if (item.title === '存货货值(亿)') {
            x.value = result.data.stockValue ? _.toNumber(result.data.stockValue / company) : 0;
            x.content[0].value1 = result.data.stockRoomNum || 0;
            x.content[0].value2 = result.data.stockParkingNum || 0;
          }
          if (item.title === '规划货值(亿)') {
            x.value = result.data.planValue ? _.toNumber(result.data.planValue / company) : 0;
            x.content[0].value1 = result.data.planRoomNum || 0;
            x.content[0].value2 = result.data.planParkingNum || 0;
          }
          if (item.title === '土储货值|香港口径(亿)') {
            x.value = result.data.soilStorageValue
              ? _.toNumber(result.data.soilStorageValue / company)
              : 0;
            x.content[0].value1 = result.data.soilStorageRoomNum || 0;
            x.content[0].value2 = result.data.soilStorageParkingNum || 0;
          }
          if (item.title === '未推售车位货值(亿)') {
            x.value = result.data.commodityValue
              ? _.toNumber(result.data.commodityValue / company)
              : 0;
            x.content[0].value2 = result.data.forsaleParkingNum || 0;
          }
        });
        yield put({
          type: 'save',
          payload: { projectIndexUsingItems: cloneProjectIndexUsingItems },
        });
      }
      // 正在进行
      const result1 = yield call(apis.getProjectUnderwayListUsingGET, payload);
      if (result1 && result1.code === 1) {
        _.each(result1.data, (item, index) => {
          const x = item;
          x.index = index + 1;
        });
        yield put({
          type: 'save',
          payload: { projectUnderwayList: result1.data },
        });
      }
      if (callback) callback();
    },
    /**
     * @description: 请求所有下拉框数据
     * @param :{projectId}
     * @return: 返回所有下拉框数据
     */
    *requestSelectData({ payload, callback }, { call, put }) {
      const businessTypeResponse = yield call(apis.getBusinessTypeUsingGET, { params: {} });
      const eventResponse = yield call(apis.getEventUsingGET, { params: {} });
      const operationByResponse = yield call(apis.getOperationByUsingGET, payload);
      const stagesResponse = yield call(apis.getStagesUsingGET_1, payload);
      const todoTypeResponse = yield call(apis.getTodoTypeUsingGET, { params: {} });
      const versionTypeResponse = yield call(apis.getVersionTypeUsingGET, { params: {} });
      if (businessTypeResponse && businessTypeResponse.code === 1) {
        yield put({
          type: 'save',
          payload: { businessType: businessTypeResponse.data },
        });
      }
      if (eventResponse && eventResponse.code === 1) {
        yield put({
          type: 'save',
          payload: { event: eventResponse.data },
        });
      }
      if (operationByResponse && operationByResponse.code === 1) {
        yield put({
          type: 'save',
          payload: { operationBy: operationByResponse.data },
        });
      }
      if (stagesResponse && stagesResponse.code === 1) {
        yield put({
          type: 'save',
          payload: { stages: stagesResponse.data },
        });
      }
      if (todoTypeResponse && todoTypeResponse.code === 1) {
        yield put({
          type: 'save',
          payload: { todoType: todoTypeResponse.data },
        });
      }
      if (versionTypeResponse && versionTypeResponse.code === 1) {
        yield put({
          type: 'save',
          payload: { versionType: versionTypeResponse.data },
        });
      }
    },
    /**
     * @description: 请求待办提醒数据
     * @param : {projectId}
     * @return: 返回待办提醒数据
     */
    *requestRemindData({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: { remindLoading: true },
      });
      const result = yield call(apis.getPorjectToDoPageUsingGET, payload);
      const remindItems = _.cloneDeep(result.data);
      const list = [];
      if (result && result.code === 1) {
        _.each(remindItems.list, (item, index) => {
          list.push({
            ...item,
            index: remindItems.pageSize * (remindItems.current - 1) + index + 1,
          });
        });
        remindItems.list = list;
        yield put({
          type: 'save',
          payload: { remindItems, remindParams: payload.params, remindLoading: false },
        });
      }
      if (callback) callback();
    },
    /**
     * @description: 请求最新动态数据
     * @param : {projectId}
     * @return: 返回最新动态数据
     */
    *requestDynamicData({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: { dynamicLoading: true },
      });
      const result = yield call(apis.getProjectNewDynamicPageUsingGET, payload);
      if (result && result.code === 1) {
        const dynamicItems = _.cloneDeep(result.data);
        const list = [];
        _.each(dynamicItems.list, (item, index) => {
          list.push({
            ...item,
            index: dynamicItems.pageSize * (dynamicItems.current - 1) + index + 1,
          });
        });
        dynamicItems.list = list;
        yield put({
          type: 'save',
          payload: { dynamicItems, dynamicParams: payload.params, dynamicLoading: false },
        });
      }
      if (callback) callback();
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default Model;
